﻿using System;
using System.Web.Mvc;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblCustomer.GetCustomer(keywords);
            return View(qry);
        }
        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var customer = id == 0
                ? new GswTblCustomer() : GswTblCustomer.SingleOrDefault(id);
            return View(customer);
        }
        [HttpPost]
        public ActionResult Create(FormCollection c, int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords="")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            if (GswTblCustomer.SingleOrDefault("where  CustomerNo='" + c["CustomerNo"] + "' and Id<>" + id + "") != null)
                ModelState.AddModelError("", "客户编号" + c["CustomerNo"].Trim() + "已经存在");
            if (GswTblCustomer.SingleOrDefault("where  CusShortName='" + c["CusShortName"] + "' and Id<>" + id + "") != null)
                ModelState.AddModelError("", "客户简称" + c["CusShortName"].Trim() + "已经存在");

            var customer = id == 0 ? new GswTblCustomer() : GswTblCustomer.SingleOrDefault(id);
            customer.CusShortName = c["CusShortName"];
            customer.CustomerName = c["CustomerName"];
            customer.Contact = c["Contact"];
            customer.Tel = c["Tel"];
            customer.Fax = c["Fax"];
            customer.Email = c["Email"];
            customer.Address = c["Address"];
            customer.BankName = c["BankName"];
            customer.BankNo = c["BankNo"];
            customer.TaxNo = c["TaxNo"];
            customer.CustomerNo = c["CustomerNo"];
            customer.CusDescrption = c["CusDescrption"];
            customer.WebUrl = c["WebUrl"];
            //customer.CreateDate = DateTime.Now;

            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                        customer.CreateDate = DateTime.Now;
                        customer.Insert();
                        customer=new GswTblCustomer();
                      
                    }
                    else
                    {
                        customer.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";

            return View(customer);

        }

        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "", string keywords="")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblCustomer.Update("set DelFlag=1 where id="+id);
            var qry = GswTblCustomer.GetCustomer(keywords);
            return View("Index", qry);
        }
    }
}
